package com.fm.financemanage.controller;

import com.fm.financemanage.component.TokenComponent;
import com.fm.financemanage.constant.ResponseEnum;
import com.fm.financemanage.constant.annotation.Require;
import com.fm.financemanage.exception.ParameterException;
import com.fm.financemanage.model.po.SysUser;
import com.fm.financemanage.model.vo.PageVo;
import com.fm.financemanage.model.vo.accountant.AccountantListVo;
import com.fm.financemanage.model.vo.accountant.AccountantOperatorVo;
import com.fm.financemanage.response.Callback;
import com.fm.financemanage.response.JsonData;
import com.fm.financemanage.service.AccountantService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotBlank;
import java.util.List;

@RequestMapping("/accountant")
@Api(tags = "会计信息")
@RestController
@Validated
public class AccountantController {

    @Autowired
    private AccountantService accountantService;

    @Autowired
    private TokenComponent tokenComponent;

    @Require
    @GetMapping("/")
    @ApiOperation("查询会计员列表")
    public JsonData<PageVo<List<AccountantListVo>>> getList(HttpServletRequest request, HttpServletResponse response,
                                                            @ApiParam(name = "name", value = "会计员名称", required = true)
                                                            @RequestParam(name = "name", required = false) String name,
                                                            @ApiParam(name = "idCard", value = "身份证,", required = true)
                                                            @RequestParam(name = "idCard", required = false) String idCard,
                                                            @ApiParam(name = "simCardId", value = "sim卡Id", required = true)
                                                            @RequestParam(name = "simCardId", required = false) Integer simCardId,
                                                            @ApiParam(name = "page", value = "页码", required = true)
                                                            @RequestParam(name = "page", required = false) Integer page,
                                                            @ApiParam(name = "limit", value = "条数", required = true)
                                                            @RequestParam(name = "limit", required = false) Integer limit
    ) {

        final String uuid = request.getAttribute("uuid").toString();
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo(), accountantService.getList(uuid, simCardId, name, idCard, page, limit));
    }

    @Require
    @PostMapping("/")
    @ApiOperation("查询会计员列表")
    public JsonData operator(HttpServletRequest request, HttpServletResponse response,
                             @RequestBody AccountantOperatorVo accountantOperatorVo, BindingResult bindingResult
    ) {
        if (bindingResult.hasErrors()) {
            throw new ParameterException(bindingResult.getAllErrors());
        }
        final String uuid = request.getAttribute("uuid").toString();
        accountantOperatorVo.setUserUuid(uuid);
        accountantService.operator(accountantOperatorVo);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
    }

    @Require
    @PostMapping("/bind/sim/{accountantUuid}")
    @ApiOperation("会计绑定sim卡")
    public JsonData bindSim(HttpServletRequest request, HttpServletResponse response,
                            @PathVariable("accountantUuid") String accountantUuid,
                            @ApiParam(name = "simCardUuid", value = "simUuid", required = true)
                            @RequestParam(name = "simCardUuid", required = false) String simCardUuid
    ) {


        accountantService.bindSim(accountantUuid, simCardUuid);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
    }

    @Require
    @DeleteMapping("/{uuid}")
    @ApiOperation("删除会计")
    public JsonData operator(HttpServletRequest request, HttpServletResponse response,
                             @PathVariable("uuid") String uuid
    ) {

        accountantService.removeById(uuid);
        return Callback.success(ResponseEnum.SUCCESS.getCode(), ResponseEnum.SUCCESS.getMemo());
    }
}
